package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ImageModel;
import com.banshuibao.bsb_android.model.KnowledgeModel;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/4.
 */

public class KnowledgeNewAdapter extends BaseAdapter {

    private Context context;

    public static final int TYPE_WEEK = 0;
    public static final int TYPE_TITLE = 1;

    private List<DataHolder> dataHolders;

    private List<KnowledgeModel.DataBean> beanList;

    private String img;

    public KnowledgeNewAdapter(Context context) {
        this.context = context;
        dataHolders = new ArrayList<>();
        beanList = new ArrayList<>();
        getImgsPost();
    }

    public void addRes(List<KnowledgeModel.DataBean> data) {
        if (beanList != null) {
            groupList(this.beanList.get(this.beanList.size() - 1), data);
            notifyDataSetChanged();
            this.beanList.addAll(data);
        }
    }

    public void setRes(List<KnowledgeModel.DataBean> data) {
        if (beanList != null) {
            this.beanList.clear();
            this.beanList.addAll(data);
            if (!TextUtils.isEmpty(img) && this.beanList.size() > 0) {
                this.beanList.get(0).setImg(img);
            }
            dataHolders.clear();
            groupList(this.beanList.get(0), this.beanList);
            notifyDataSetChanged();
        }
    }

    private void groupList(KnowledgeModel.DataBean dataBean, List<KnowledgeModel.DataBean> list) {
        if (this.beanList == null || this.beanList.size() == 0)
            return;
        String date = TimeUtil.format2(dataBean.getCreateDate());
        int size = list.size();
        for (int i = 0; i < size; ++i) {
            KnowledgeModel.DataBean dataBean1 = list.get(i);
            String date1 = TimeUtil.format2(dataBean1.getCreateDate());
            if (dataHolders.size() == 0) {
                createWeekData(list.get(0));
            }
            if (!TextUtils.equals(date, date1)) {
                createWeekData(dataBean1);
                date = date1;
            }
            createData(dataBean1);
        }
    }

    private void createData(KnowledgeModel.DataBean dataBean) {
        TitleDataHolder titleDataHolder = new TitleDataHolder();
        titleDataHolder.setObj(dataBean);
        dataHolders.add(titleDataHolder);
    }

    private void createWeekData(KnowledgeModel.DataBean dataBean) {
        WeekDataHolder titleDataHolder = new WeekDataHolder();
        titleDataHolder.setObj(dataBean);
        dataHolders.add(titleDataHolder);
    }

    public void setImg(String img) {
        if (dataHolders != null && dataHolders.size() > 1) {
            if (dataHolders.get(1) instanceof TitleDataHolder) {
                TitleDataHolder dataHolder = (TitleDataHolder) dataHolders.get(1);
                KnowledgeModel.DataBean dataBean = dataHolder.getObj();
                dataBean.setImg(img);
                //
                notifyDataSetChanged();
            }
        }
    }

    private void getImgsPost() {
        HttpUtil.doPost(Constants.Url.KNOWLEDGE_IMAGE, null, new HttpResponse(context, ImageModel.class) {
            @Override
            public void onResult(Object obj) {
                ImageModel s = (ImageModel) obj;
                String data = s.getData();
                img = data;
                setImg(data);
            }
        });
    }

    @Override
    public int getCount() {
        return dataHolders.size();
    }

    @Override
    public DataHolder getItem(int i) {
        return dataHolders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        return dataHolders.get(position).getViewType();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int itemViewType = getItemViewType(i);
        switch (itemViewType) {
            case TYPE_WEEK:
                return getWeekView(i, view, viewGroup);
            case TYPE_TITLE:
                return getTitleView(i, view, viewGroup);
        }
        return view;
    }

    private View getTitleView(int i, View view, ViewGroup viewGroup) {
        ViewHolder2 holder2 = null;
        if (view == null) {
            holder2 = new ViewHolder2();
            view = LayoutInflater.from(context).inflate(R.layout.item_knowledge_case_people, viewGroup, false);
            holder2.content = (TextView) view.findViewById(R.id.case_content);
            holder2.look = (TextView) view.findViewById(R.id.case_look);
            holder2.date = (TextView) view.findViewById(R.id.case_date);
            holder2.people = (TextView) view.findViewById(R.id.case_people);
            holder2.img = (ImageView) view.findViewById(R.id.img);
            holder2.relativeLayout = (RelativeLayout) view.findViewById(R.id.relative);
            view.setTag(holder2);
        } else holder2 = (ViewHolder2) view.getTag();

        KnowledgeModel.DataBean bean = (KnowledgeModel.DataBean) dataHolders.get(i).getObj();
        if (bean != null) {
            holder2.content.setText(bean.getTitle());
            holder2.look.setText(String.valueOf(bean.getClickNum()));
            long createDate = bean.getCreateDate();
            String time = null;
            if (createDate != 0) {
                time = TimeUtil.format(createDate);
            } else {
                time = "";
            }
            holder2.date.setText(time);
            holder2.people.setText(bean.getAuthor());
            if (!TextUtils.isEmpty(bean.getImg())) {
                holder2.img.setVisibility(View.VISIBLE);
                ImageUtil.loadImage(context, bean.getImg(), holder2.img);
            } else {
                holder2.img.setVisibility(View.GONE);
            }
            final String uuid = bean.getUuid();
            holder2.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!TextUtils.isEmpty(uuid)) {
                        IntentManager.goKnowledgeDetailsAcitivty(context, uuid, 1);
                    }
                }
            });
        }
        return view;
    }

    private View getWeekView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 holder1 = null;
        if (view == null) {
            holder1 = new ViewHolder1();
            view = LayoutInflater.from(context).inflate(R.layout.item_week, viewGroup, false);
            holder1.date = (TextView) view.findViewById(R.id.week_date);
            holder1.week = ((TextView) view.findViewById(R.id.week_date2));
            view.setTag(holder1);
        } else
            holder1 = (ViewHolder1) view.getTag();

        KnowledgeModel.DataBean bean = (KnowledgeModel.DataBean) dataHolders.get(i).getObj();
        if (bean != null) {
            long createDate = bean.getCreateDate();
            String date = TimeUtil.format2(createDate);
            holder1.date.setText(date);
            String week = TimeUtil.getWeek(createDate);
            holder1.week.setText(week);
        }
        return view;
    }

    private class ViewHolder1 {
        TextView date;
        TextView week;
    }

    private class ViewHolder2 {
        TextView content;
        TextView look;
        TextView date;
        TextView people;
        ImageView img;
        RelativeLayout relativeLayout;
    }

    public abstract class DataHolder<T> {
        private T obj;

        public T getObj() {
            return obj;
        }

        public void setObj(T obj) {
            this.obj = obj;
        }

        public abstract int getViewType();
    }

    private class WeekDataHolder extends DataHolder<KnowledgeModel.DataBean> {

        @Override
        public int getViewType() {
            return TYPE_WEEK;
        }
    }

    private class TitleDataHolder extends DataHolder<KnowledgeModel.DataBean> {

        @Override
        public int getViewType() {
            return TYPE_TITLE;
        }
    }
}
